<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="taglibs.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="${path}/static/bootstrap-3.3.7-dist/css/bootstrap.css"/>
<link rel="stylesheet" href="${path}/static/layui/css/layui.css" media="all">
</head>
<body>

	<form class="layui-form layui-form-pane" id="insert_form"  method="post" onsubmit="return false">
	  
	  <div class="layui-form-item">
	    <label class="layui-form-label">姓名</label>
	    <div class="layui-input-inline">
	      <input name="name" id="name" class="layui-input" type="text" placeholder="请输入姓名" autocomplete="off">
	    </div>
	    <div class="layui-form-mid layui-word-aux">请务必填写学生姓名</div>
	  </div>
	  
	  <div class="layui-form-item">
	    <label class="layui-form-label">年龄</label>
	    <div class="layui-input-inline">
	      <input name="age" class="layui-input" type="text" placeholder="请输入年龄" autocomplete="off" lay-verify="required">
	    </div>
	  </div>
	  
	  <div class="layui-form-item" pane="">
	    <label class="layui-form-label">性别</label>
	    <div class="layui-input-block">
	      <input name="gender" title="男" type="radio" checked="" value="男">
	      <input name="gender" title="女" type="radio" value="女">
	    </div>
	  </div>
	  
	  <div class="layui-form-item" pane="">
	    <label class="layui-form-label">出生日期</label>
	    <div class="layui-input-block">
	      <input type="text" name="birthday" id="birthdayId" autocomplete="off" class="layui-input" placeholder="yyyy-MM-dd">
	    </div>
	  </div>
	  
	     <div class="layui-form-item">
		    <label class="layui-form-label">班级</label>
		    <div class="layui-input-inline">
			      <select id="classId" name="className">
			        <option value="">请选择班级</option>
			      </select>
    		</div>
    	</div>
    
	  <div class="layui-form-item">
	    <button class="layui-btn" onclick="submitInsert()">添加</button>
	  </div>
	</form>
	
	
	<script src="${path}/static/jquery-1.11.3.js"></script>
	<script src="${path}/static/layui/layui.js"></script>
	<script src="${path}/static/mylayer.js"></script>
	<script src="${path}/static/util.js"></script>
	
	<script>
		layui.use(['form','laydate','layer'], function(){
		  var form = layui.form;
		  var layer = layui.layer;
		  var laydate = layui.laydate;
			  
		  laydate.render({
			 elem: '#birthdayId'
			 ,trigger: 'click'
		  });
		  
	  });
		
		$(function(){
			$.post(
				"${path}/classes/selectClass.action",
				function(jsonArray){
					console.log(jsonArray);
					$(jsonArray).each(function(){
						var html = "<option value='"+this.id+"'>"+this.name+"</option>";
						$("#classId").append(html);
					});
				},
				"json"
			);
		});
		
		function submitInsert(){
			var nameValue = $("#name").val();
			if(util.isEmpty(nameValue)){
				mylayer.errorMsg("姓名不能为空");
				return;
			}
			
			 $.ajax({
	    		  url:"${path}/student/insert.action",
	    		  data:$("#insert_form").serialize(),
	    		  dataType:"json",
	    		  type:"post",
	    		  success:function(jsonObj){
	    			  if(jsonObj.code == util.SUCCESS){
	    				  mylayer.successMsg(jsonObj.msg);
	    				  //当你在iframe页面关闭自身时
						  var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
						  setTimeout(function(){
								parent.layer.close(index); //再执行关闭 
								// 刷新父页面
								window.parent.location.reload();
						  },1000);
	    			  } else {
	    				  mylayer.errorMsg(jsonObj.msg);
	    			  }
	    		  }
			});
		}
	</script>
</body>
</html>